package cn.how2j.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import cn.how2j.Person;


/**
 * JDBC link
 * @author giao
 *
 */
public class DBSConn {
	//Connect
	String url = "jdbc:mysql://localhost:3306/manager?allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
 
 
	String username = "student";
	String password = "st_123456";
	Connection con;  //����һ��Connection
	PreparedStatement statement=null;   
	public void dbsConn(Person person) {  
		try {
 
			Class.forName("com.mysql.cj.jdbc.Driver");
			con=DriverManager.getConnection(url,username,password);
			String sql="insert into t_hero(name,id,level) values(?,?,?)";
 
			statement=con.prepareStatement(sql);
			statement.setString(1,person.getName());
			statement.setInt(2, person.getAge());
			statement.setString(3, person.getDepartment());
			statement.executeUpdate();
			System.out.print("�ɹ���¼");
//			statement.close();
//			con.close();
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch(SQLException e2) {
			e2.printStackTrace();
		}
		finally {
			if(statement!=null) {
				try {
					statement.close();
				}catch(SQLException e)
				{
					
				}
			}
			if(con!=null) {
				try {
					con.close();
				}catch(SQLException e)
				{
					
				}
			}
		}
	}
}
